//
//  ShowOrderViewController.m
//  YunGou
//
//  Created by macbook on 16/5/17.
//  Copyright © 2016年 macbook. All rights reserved.
// 晒单分享

#import "ShowOrderViewController.h"
#import "ShowOrderDetailViewController.h"
#import "NSObject+Tools.h"
#import "ShowOrderModel.h"
#import "ShowOrderCell.h"
#import "NullDataView.h"
#import "NSString+Frame.h"
#import <MJRefresh.h>

@interface ShowOrderViewController ()
@property(nonatomic,strong)NSMutableArray *showOrderArray;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong) NullDataView *nullDataView;
@end

@implementation ShowOrderViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _showOrderArray = [NSMutableArray array];
    _page = 0;
    [self loadData];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self loadData];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 0;
        _showOrderArray = [NSMutableArray array];
        [self loadData];
    }];
    
}

- (void)loadData{
    if (!_isShare) {
        [MBProgressHUD showMessage:@"正在加载"];
        [NetWorkTool shaiDanWithWithIsMine:_isMine start:_page * 10 Success:^(id object) {
            [MBProgressHUD hideHUD];
            if ([object[@"code"] integerValue] == 1) {
                for (NSDictionary *dict in object[@"data"]) {
                    ShowOrderModel *showOrderModel = [ShowOrderModel modelWithDict:dict];
                    [_showOrderArray addObject:showOrderModel];
                }
            }
            
            if (_showOrderArray.count == 0) {
                [self.view addSubview:self.nullDataView];
            }else{
                if (_nullDataView) {
                    [_nullDataView removeFromSuperview];
                    _nullDataView = nil;
                }
            }
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
    
        }];
    }else{
        [MBProgressHUD showMessage:@"正在加载"];
        [NetWorkTool shaiDanFenXiangWithGoodsId:_goodsId start:_page * 10 success:^(id object) {
            [MBProgressHUD hideHUD];
            if ([object[@"code"] integerValue] == 1) {
                for (NSDictionary *dict in object[@"data"]) {
                    ShowOrderModel *showOrderModel = [ShowOrderModel modelWithDict:dict];
                    [_showOrderArray addObject:showOrderModel];
                }
            }
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _showOrderArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShowOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.showOrderModel = _showOrderArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShowOrderModel *showOrderModel = _showOrderArray[indexPath.row];
    NSString *goodsName = [NSString stringWithFormat:@"获得奖品：%@", showOrderModel.title];
    CGFloat goodsHeight = [goodsName heightWithFont:[UIFont systemFontOfSize:12] withinWidth:ScreenWidth - 80] > 14.5?[goodsName heightWithFont:[UIFont systemFontOfSize:12] withinWidth:ScreenWidth - 80] - 14.5:0;
        CGFloat height = [showOrderModel.sd_content heightWithFont:[UIFont systemFontOfSize:16] withinWidth:ScreenWidth - 80];
    
    if ([showOrderModel.sd_photolist isEqualToString:@""]) {
        return 193 - 38.5 - 73.5 + height + goodsHeight;
    }return  193 - 38.5 + height + goodsHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShowOrderDetailViewController *showOrderDetailVC = SB(@"Common", @"ShowOrderDetailVC");
    showOrderDetailVC.showOrderModel = _showOrderArray[indexPath.row];
    [self.navigationController pushViewController:showOrderDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
